package
{
	import events.BuildingEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class BuildingDemo extends Sprite
	{

		private var buildingSelector:BuildingSelector;
		private var crtBase:Base;
		public function BuildingDemo()
		{
			init();
		}
		
		private function init():void
		{
			// 初始化背景
			var bg:Image = new Image();
			this.addChild( bg );
			bg.loadImage("assets/background.jpg");
			// 初始化地块
			for(var i:int=0; i<3;i++)
			{
				var base:Base = new Base();
				this.addChild( base );
				base.y = 200;
				base.x = 40 + 120*i;
				
				base.addEventListener(MouseEvent.CLICK, onBaseClick);
			}
			// 初始化建筑物选择器
			buildingSelector = new BuildingSelector();
			this.addChild( buildingSelector );
			buildingSelector.visible = false;
			
			buildingSelector.addEventListener(BuildingEvent.SELECTED, onSelected);
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function onSelected(event:BuildingEvent):void
		{
			trace( event.url );
			// 更换地块加载的建筑物图片
			crtBase.loadImage( event.url );
			buildingSelector.visible = false;
		}
		/**
		 * 地块点击处理函数 
		 * @param event
		 * 
		 */		
		protected function onBaseClick(event:MouseEvent):void
		{
			crtBase = event.currentTarget as Base;
			buildingSelector.visible = true;
		}
	}
}